//
//  XS_OrderVC.swift
//  XueDao
//
//  Created by Mac on 2020/12/21.
//

import UIKit

class XS_OrderVC: XS_BaseVC {
    
    lazy var channelArray = ["全部", "已支付","未支付"]
    lazy var pageView = getPageView()
    lazy var segment = getSegment()
    lazy var topScroll = UIScrollView()
    lazy var line = UIView()
    
    var vcArray : [XS_OrderSubVC] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        title = "订单"
        topScroll.bounces = false
        topScroll.showsHorizontalScrollIndicator = false
        topScroll.clipsToBounds = true
        topScroll.frame = CGRect(x: 0, y: 0, width: .screenWidth, height: .scale*44)
        view.addSubview(topScroll)
        
        line.backgroundColor = #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 1)
        line.layer.cornerRadius = .scale
        line.frame = CGRect(x: 0, y: .scale*42, width: .scale*33, height: .scale*2)
        line.tz_centerX = .screenWidth / 6
        topScroll.addSubview(line)
        
        initSubVC()
        
        reloadTop()
        
//        view.addSubview(segment)
        view.addSubview(pageView)
    }
    
    func initSubVC(){
        let subVC1 = XS_OrderSubVC()
        subVC1.nav = navigationController
        subVC1.type = .all
        vcArray.append(subVC1)
        let subVC2 = XS_OrderSubVC()
        subVC2.nav = navigationController
        subVC2.type = .payment
        vcArray.append(subVC2)
        let subVC3 = XS_OrderSubVC()
        subVC3.nav = navigationController
        subVC3.type = .nopay
        vcArray.append(subVC3)
    }
    
    func reloadTop() {
        for view in topScroll.subviews {
            if view.isKind(of: UIButton.self) {
                view.snp.removeConstraints()
                view.removeFromSuperview()
            }
        }
        var last: UIView?
        for (index, text) in channelArray.enumerated() {
            let btn = XS_NoHighlightBtn()
            btn.setTitleColor(#colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1), for: .selected)
            btn.setTitleColor(#colorLiteral(red: 0.5333333333, green: 0.5568627451, blue: 0.5764705882, alpha: 1), for: .normal)
            btn.titleLabel?.font = .systemFont(ofSize: 18, weight: .medium)
            btn.setTitle(text, for: .normal)
            btn.tag = index + 100
            btn.rx.tap.asDriver()
                .drive(onNext: { [weak self] in
                    self?.pageView.changeToItem(at: index)
                    self?.didScroll(to: index)
                }).disposed(by: disposeBag)
            topScroll.addSubview(btn)
            btn.snp.makeConstraints { m in
                m.top.height.equalToSuperview()
                if let last = last {
                    m.left.equalTo(last.snp.right)
                } else {
                    m.left.equalToSuperview()
                    btn.isSelected = true
                }
                m.width.equalTo(CGFloat.screenWidth / 3)
            }
            last = btn
        }
        pageView.reloadData()
        pageView.changeToItem(at: 0)
    }

}

extension XS_OrderVC: JXSegmentDelegate,JXPageViewDataSource,JXPageViewDelegate {
    
    func getSegment() -> JXSegment {
        let segment = JXSegment(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
        segment.scrollView.contentInsetAdjustmentBehavior = .never
        segment.updateChannels(channelArray)
        return segment
    }
    
    func getPageView() -> JXPageView {
        let pageView = JXPageView(frame: CGRect(x: 0, y: topScroll.qmui_bottom, width: .screenWidth, height: .screenHeight - .barHeight - topScroll.qmui_bottom - 44))
        pageView.scrollview.contentInsetAdjustmentBehavior = .never
        pageView.datasource = self
        pageView.delegate = self
        return pageView
    }
    
    
    func jxSegment(_ segment: JXSegment!, didSelect index: Int) {
        pageView.changeToItem(at: index)
    }
    
    func numberOfItem(in pageView: JXPageView!) -> Int {
        channelArray.count
    }
    
    func pageView(_ pageView: JXPageView!, viewAt index: Int) -> UIView! {
        return vcArray[index].view
    }
    
    func didScroll(to index: Int) {
        segment.didChenge(to: index)
        for view in topScroll.subviews {
            if let btn = view as? UIButton {
                btn.layoutIfNeeded()
                if btn.tag == 100 + index {
                    btn.isSelected = true
                    line.center.x = btn.center.x
                } else {
                    btn.isSelected = false
                }
            }
        }
    }
    
    
}

